﻿using System.Data;
using System.Web;
using System.Web.Script.Serialization;
using Core.Library;
using DataObject;
using DataObject.Cache;
using DataObject.Members;
using DataObject.Photo;
using DataObject.QA;
using DataObject.Tag;

namespace CMS.Handlers
{
    /// <summary>
    /// Summary description for EventHandler
    /// </summary>
    public class EventHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string command = "";
            if (context.Request.RequestType == "POST")
            {
                if (context.Request["command"] != null)
                {
                    command = context.Request["command"];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (context.Request.QueryString["command"] != null)
                {
                    command = context.Request.QueryString["command"];
                }
                else
                {
                    return;
                }
            }

            switch (command.ToLower())
            {
                case "taggetall":
                    TagGetAll(context);
                    break;
                case "taggetbyid":
                    TagGetByIdTypeId(context);
                    break;
                case "checktagvalid":
                    CheckTagValid(context);
                    break;
                case "addtag":
                    AddTag(context);
                    break;
                case "bindqabyreplyto":
                    BindQAByReplyTo(context);
                    break;
                case "memberalbumbyid":
                    MemberAlbumById(context);
                    break;
                case "memberupdateishomepage":
                    MemberUpdateIsHomepage(context);
                    break;
                case "memberupdateisslide":
                    MemberUpdateIsSlide(context);
                    break;
            }
        }

        private void MemberUpdateIsSlide(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = ConvertUtility.ConvertTo<int>(context.Request[Utils.Id]);
                if (id > 0)
                {
                    MembersController.Members_Update_IsSlide(id);
                    //CacheController.RemoveCachePort(Constants.ManagerCacheGroup.User);
                }
            }
        }

        private void MemberUpdateIsHomepage(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = ConvertUtility.ConvertTo<int>(context.Request[Utils.Id]);
                if (id > 0)
                {
                    MembersController.Members_Update_IsHomePage(id);
                    //CacheController.RemoveCachePort(Constants.ManagerCacheGroup.User);
                }
            }
        }

        private void MemberAlbumById(HttpContext context)
        {
            if (context.Request[Utils.Id] != null && context.Request[Utils.Round] != null)
            {
                var id = ConvertUtility.ConvertTo<int>(context.Request[Utils.Id]);
                var round = ConvertUtility.ConvertTo<int>(context.Request[Utils.Round]);
                if(id > 0 && round > 0)
                {
                    var lst = PhotoController.CMS_Photos_GetByMemberID(id, round);
                    var js = new JavaScriptSerializer();
                    var json = js.Serialize(lst);
                    context.Response.Write(json);
                }
            }
        }

        public void BindQAByReplyTo(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = ConvertUtility.ConvertTo<int>(context.Request[Utils.Id]);

                if (id > 0)
                {
                    var lst = QAController.Answer_GetListByReplyTo(id);
                    if (lst != null)
                    {
                        var objSerial = new JavaScriptSerializer();
                        var json = objSerial.Serialize(lst);
                        context.Response.Write(json);
                    }
                }
            }
        }

        private void AddTag(HttpContext context)
        {
            if (context.Request[Utils.Tag] != null && context.Request[Utils.UserName] != null)
            {
                var tag = context.Request[Utils.Tag];
                var username = context.Request[Utils.UserName];
                var o = new TagObject();
                o.Name = tag;
                o.CreatedBy = username;
                var result = TagController.Tag_Create(o);
                var objSerial = new JavaScriptSerializer();
                var json = objSerial.Serialize(result);
                context.Response.Write(json);
            }
        }

        private void CheckTagValid(HttpContext context)
        {
            if (context.Request[Utils.Tag] != null)
            {
                var tag = context.Request[Utils.Tag];
                var result = TagController.Tag_CheckNameExist(0, tag);
                var objSerial = new JavaScriptSerializer();
                var json = objSerial.Serialize(result);
                context.Response.Write(json);
            }
        }

        private void TagGetByIdTypeId(HttpContext context)
        {
            if (context.Request[Utils.Id] != null && context.Request[Utils.TypeId] != null)
            {
                var id = ConvertUtility.ConvertTo<long>(context.Request[Utils.Id]);
                var typeId = ConvertUtility.ConvertTo<int>(context.Request[Utils.TypeId]);
                if (id > 0 && typeId >= 0)
                {
                    var result = TagController.Tag_GetById_TypeId(id, typeId);
                    if (result != null)
                    {
                        var dt = new DataTable();
                        dt.Columns.Add("name");
                        dt.Columns.Add("id");
                        foreach (var i in result)
                        {
                            dt.Rows.Add(i.Name, i.TagId);
                        }
                        var json = ConvertUtility.DataTableToJSON(dt);
                        context.Response.Write(json);
                    }
                }
            }
        }

        private void TagGetAll(HttpContext context)
        {
            var result = TagController.Tag_GetAll();
            if (result != null)
            {
                var dt = new DataTable();
                dt.Columns.Add("name");
                dt.Columns.Add("id");
                foreach (var i in result)
                {
                    dt.Rows.Add(i.Name, i.TagId);
                }
                var json = ConvertUtility.DataTableToJSON(dt);
                context.Response.Write(json);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}